// pages/mqtt/index.js
const App = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        clientId: App.data.options.clientId,
        ts: "",
        sportData: {},
        deviceCode: "",
        sportId: "",
        userKey: "",
        productName: "",
        bind: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log("mqtt onLoad")
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        console.log("mqtt onReady")
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        console.log("mqtt onShow")
        this.initListen()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {
        console.log("mqtt onHide")
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        console.log("mqtt onUnload")
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        console.log("mqtt onPullDownRefresh")
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        console.log("mqtt onReachBottom")
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {
        console.log("mqtt onShareAppMessage")
    },
    initListen() {
        App.data.mqttClient.on('message', (topic, message) => {
            // console.log("↓", topic, "\n", JSON.stringify(JSON.parse(message), null, 4))
            if (topic.endsWith(this.data.deviceCode)) {
                const {data: sportData, ts, productName} = JSON.parse(message)
                const {sportId, userKey, deviceCode} = sportData
                this.setData({
                    ts,
                    sportData,
                    sportId,
                    userKey,
                    deviceCode,
                    productName
                })
            }
        })
    },
    onSubscribe(event) {
        let deviceCode = this.data.deviceCode
        if (!deviceCode) {
            wx.showToast({
                title: '设备编号必填',
                icon: 'error',
                mask: true
            })
            return
        }
        App.subscribe([`infcompetition/outdoor/+/instant/${deviceCode}`], 1).then(res => {
            wx.showToast({
                title: '订阅成功',
                icon: "success",
                mask: true
            })
            this.setData({
                bind: true
            })
        }).catch(err => {
            wx.showToast({
                title: '订阅失败',
                icon: "error",
                mask: true
            })
        })
    },
    onUnSubscribe(event) {
        App.unSubscribe([`infcompetition/outdoor/+/instant/${this.data.deviceCode}`]).then(res => {
            this.onClear()
            wx.showToast({
                title: '取消订阅成功',
                icon: "success",
                mask: true
            })
            this.setData({
                bind: false
            })
        }).catch(err => {
            wx.showToast({
                title: '取消订阅失败',
                icon: "success",
                mask: true
            })
        })
    },
    onClear() {
        this.setData(({
            sportData: {},
            ts: "",
            sportId: "",
            userKey: "",
            productName: ""
        }))
    },
    bindInput(e) {
        this.setData({
            deviceCode: e.detail.value
        })
    }
})
